﻿/* ------------------------------------------------------------------------------------------------------------
文件名:	Lib.js

用	途：该文件存放公共函数，是个JS文件的支持所需

创建者：Randy
--------------------------------------------------------------------------------------------------------------*/


//系统配置信息

var Medi_VirtualDir = "Web";   //站点的虚拟目录



//去掉字符左右两边的空格

String.prototype.Trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
//去掉字符串中的<html>标签 
String.prototype.stripTags = function(){ 
    return this.replace(/<\/?[^>]+>/gi, ''); 
}
//把字符串转换为HTML编码
String.prototype.escapeHTML = function(){ 
    var div = document.createElement('div'); 
    var text = document.createTextNode(this); 
    div.appendChild(text); 
    return div.innerHTML; 
}
//把HTML转换为字符串
String.prototype.unescapeHTML = function(){ 
    if(this == "") return "";
    var div = document.createElement('div'); 
    div.innerHTML = this.stripTags(); 
    return div.childNodes[0].nodeValue; 
}
//把字符传转换为短日期格式 2005-10-10  2005/10/10
String.prototype.ToShortDateString = function()
{
	var p = this.split(" ");
	return p[0];
}
//同C#中的Format   eg.   var str = '23423{0}234234{1}44444';str.Format('a','b')
String.prototype.Format = function(){
	var str = this;	
	for(var i = 0; i < arguments.length;i++){
	    var re = "/{" + i + "\\}/g";
	    var method = "str.replace("+re+",'" + arguments[i] +"')"; 	    
	    str = eval(method);	
	}	
	return str;
}
//把字符串转换为日期对象

String.prototype.ToDateTime = function()
{
	var date = this;
	
	var ary = date.split("-");
	
	var nDate = new Date(ary[0],ary[1],ary[2]);
	return nDate;
}

//把字符串转换成HTML格式
String.prototype.TextToHtml = function()
{
    var str = this;      
    str = str.replace(/\n/img,"●");
    str = str.replace(/ /img,"■");    
    str = str.escapeHTML(); //该方法只能转化部分HTML，不包含换行符号    
    str = str.replace(/●/img,"<br>");
    str = str.replace(/■/img,"&nbsp;"); 
    
    return str;
}

//把HTML转换成字符串
String.prototype.HtmlToText = function()
{
    var str = this;
    str = str.replace(/<br>/img,"●");
    str = str.replace(/&nbsp;/img,"■");  
    str = str.unescapeHTML();   //该方法只能转化部分HTML，不包含换行符号    
    str = str.replace(/●/img,"\n");
    str = str.replace(/■/img," "); 
    
    return str;
}

/*
功  能：测试内容的有效性


用  法：如果第一个参数错误，那么就执行第二个参数，直到成功为值

        Try.these(
				function() {return new ActiveXObject('Msxml2.XMLHTTP')},
				function() {return new ActiveXObject('Microsoft.XMLHTTP')},
				function() {return new XMLHttpRequest()}

创建者：Randy

时  间：2006-8-3
*/
var Try = {
	these: function() 
	{
		var returnValue;
	
		for (var i = 0; i < arguments.length; i++) {
			var lambda = arguments[i];
			try {
				returnValue = lambda();
				break;
			} catch (e) {}
		}
	
		return returnValue;
	}
}

/*
功  能：获取对象数组
用  法：$('id1','id2','id3')
参  数：对象ID
创建者：Randy
时  间：2006-8-23

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;
      
    elements.push(element);
  }

  return elements;
}
*/
/*
功  能：获取对象数组
用  法：$n('id1')
参  数：1 对象名称
创建者：Randy
时  间：2006-8-23
*/
function $n() {
    return document.getElementsByName(arguments[0]);
}

/*
功  能：获取对象数组
用  法：$tn("sTagName")
参  数：1 HTML标签名称  eg:  $tn('INPUT'),$tn('DIV')
创建者：Randy
时  间：2006-10-26
*/
function $tn(sTagName)
{
    return document.getElementsByTagName(sTagName)
}

/*
功  能：获取对象数组
用  法：$t("typeName")
参  数：1 控件的类型名称

创建者：Randy
时  间：2006-10-26
*/
function $t(sTypeName)
{
    var inputs = $tn("INPUT");
    var objs = new Array();
    
    for(var i = 0;i < inputs.length;i++)
    {
        if (inputs[i].type.toLowerCase() == sTypeName) {            
            objs.push(inputs[i]);
        }
    }
    return objs;
}

function $type(typeName){
    return $t(typeName);
}



/*
功  能：获取目标对象所在的行对象

用  法：$r('id1')
参  数：1 对象名称
创建者：Randy
时  间：2006-9-4
*/
function $row(e)
{
    var tr = e;
    while (tr.tagName.toLowerCase() != "tr") {
        tr = tr.parentNode;
    } 
    return tr;
}

/*
功  能：Get RadiobuttonList Selected Value
用  法：$rblValue('rblObject')
参  数：
创建者：Randy
时  间：2006-9-4
*/
function $rblValue(e)
{
	var oRbl = $n(e);
 	var rblValue = "";
	
	for(var i = 0; i < oRbl.length;i++)
	{
		if(oRbl[i].checked)
		{
			rblValue = oRbl[i].value;
			return rblValue;
		}
	}
	return ""
}




/*
功  能：获取目标对象所在的表格对象
用  法：$tbl(objForTbl)
参  数：1 对象名称
创建者：Randy
时  间：2006-11-9
*/
function $tbl(e)
{
    var tbl = e;
    while (tbl.tagName.toLowerCase() != "table") {
        tbl = tbl.parentNode;
    } 
    return tbl;
}

function setDisable(e,flag)
{
    var obj = $(e);
    if(obj == null) return;   
        
    flag = flag == null?true:flag;
    obj.disabled = true;    
    if(obj.tagName.toLowerCase() == "a")
    {
        obj.onclick = function(){
            return false;
        }
    }   
}



var Lib = {};

/*

添加小图片按钮
等同于原始图片
*/
Lib.addEqualsSmallButton = function(panel, srcID, className) {
    if ($("#" + srcID).length == 0) return;

    var btn = document.createElement("span");
    btn.className = className;
    btn.title = $("#" + srcID).val();
    btn.onclick = function() {
        document.getElementById(srcID).click();
    }

    $("#" + panel).append(btn);
}

/*

mode 显示方式  0 弹出提示框、 1 *号提示、 2 为0，1两种方式 
*/
Lib.ValidateEquals = function(src,val1,val2,tips,mode){

    if(val1 == val2){
    
        if(mode == 0){
            alert(tips);
        }
        else if(mode == 1)
        {
            if(src.next("#myTips").length == 0)
                src.after("<span id='myTips' class='imgError' title='"+tips+"'></span>"); 
            else
                src.next("#myTips")[0].style.display = "";
                
        }
        else if(mode == 2)
        {
            Lib.ValidateEquals(src,val1,val2,tips,0);
            Lib.ValidateEquals(src,val1,val2,tips,1);
        }
    
        return true;
    }
    else{
        if(src.next("#myTips").length != 0)
            src.next("#myTips")[0].style.display = "none";
    
        return false;
    }
}


//打开主页面


//在tab上打开窗体,隐藏tab   visible 是否显示tab 标题  默认false
Lib.addMidWindow = function(name,url,visible){
    if(visible == undefined) visible = false;
    parent.addMidWindow(name,url,visible);
}

//关闭当前tab窗口
Lib.CloseMidWindow = function(){
    parent.closeCurrentTab();
}

Lib.OpenMainWin = function()
{

}

//打开一个新窗体，窗体居中
/*
参数：
    sURL 网页地址
    w 窗口的宽度
    h 窗口的高度
    setting 设置的参数，枚举值
    {
        scrollbars:"yes"|"no",
        menubar:"yes"|"no",
        status:"yes"|"no",
        toolbar:"yes"|"no",
        resizable:"yes"|"no",
        location:"yes"|"no"  
    }



*/
Lib.OpenWin = function(sURL,w,h)
{
    var sw = parseInt(screen.availWidth);
    var sh = parseInt( screen.availHeight);

    if(w > sw) w = sw;
    if(h > sh) h = sh;

    var left = (sw - w)/2;
    var top = (sh - h)/2;
    
    
    var scrollbars = "yes";
    var menubar = "yes";
    var status = "yes";
    var toolbar = "yes";
    var resizable = "yes";
    var location = "yes";
    
    //取系统参数
    if(arguments.length == 4)
    {
        var arg = arguments[3];        
        
        scrollbars = arg.scrollbars != null?arg.scrollbars:"no";
        menubar = arg.menubar != null?arg.menubar:"no";
        status = arg.status != null?arg.status:"no";
        toolbar = arg.toolbar != null?arg.toolbar:"no";
        resizable = arg.resizable != null?arg.resizable:"no";
        location = arg.location != null?arg.location:"no";       
    }

    var sFeatures = "height={0},width={1},left={2},top={3},status={6},toolbar={7},resizable={8},scrollbars={4},menubar={5},location={9}";
    sFeatures = sFeatures.Format(h,w,left,top,scrollbars,menubar,status,toolbar,resizable,location)


    var name = Math.random() + "fff";
    name = name.substr(name.indexOf(".") + 1);
    
   // alert(name);
    window.open(sURL,name,sFeatures);    
    
    //window.open(sURL);
}

//把时间转换为　毫秒
Lib.ToTimeString = function(tmpInt)
{
    var totalSecond = tmpInt;
    var iHour = parseInt(totalSecond / (60*60));
    var iMinute = parseInt((totalSecond - iHour*60*60) / 60);
    var iSecond = (totalSecond - iHour*60*60 - iMinute * 60);

    return iHour + ":" + iMinute + ":" + iSecond;
}